package org.example.fboot.controller.auth;

import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.example.fboot.config.jwt.AccessComponent;
import org.example.fboot.dto.User;
import org.example.fboot.result.Result;
import org.example.fboot.service.AuthService;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.server.ServerWebExchange;
import reactor.core.publisher.Mono;

@Slf4j
@RestController
@RequestMapping("/auth")
public class AuthController {
    @Resource
    private AuthService authService;
    @Resource
    private AccessComponent accessComponent;

    @PostMapping("/login")
    public Mono<Result<String>> login(ServerWebExchange exchange, @RequestBody @Validated User user) {
        return authService.login((result)-> accessComponent.createToken(exchange,result),user);
    }
    @PostMapping("/register")
    public Mono<Result<String>> register(ServerWebExchange exchange, @RequestBody @Validated User user){
        return authService.registerUser((result)-> accessComponent.createToken(exchange,result),user);
    }
}
